using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.Properties;

namespace Workflows.Components
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class GlobalVariableGetterComponent:WorkflowComponentBase
    {
        #region props
        private GlobalVariableGetterInstruction _Instruction;
        private GlobalVariableGetterOutput _Output;

        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction = (GlobalVariableGetterInstruction) value; }
        }

        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output = (GlobalVariableGetterOutput) value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public List<string> GlobalVariableNames
        {
            get { return this._Instruction.GlobalVariableNames; }
            set { this._Instruction.GlobalVariableNames = value; }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public GlobalVariableGetterComponent():base()
        {
            this.ComponentName = "Global variable getters";
            this.Description = "Get values of global variables of this workflow";
            this.ComponentIcon = Resources.bind;
            this.ComponentImage = Resources.Control;
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.Misc.ToString();

            this._Instruction = new GlobalVariableGetterInstruction(this.ComponentID, Guid.Empty, null);
            this._Output = new GlobalVariableGetterOutput(this.ComponentID, Guid.Empty, null);
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            if (this.GlobalVariableNames != null && this.GlobalVariableNames.Count > 0)
            {
                XmlNode varRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "Getters");
                foreach (string varName in this.GlobalVariableNames)
                {
                    XmlNode getterNode = XmlDataUtil.AddElement(ref xDoc, varRootNode, "Getter");
                    XmlDataUtil.UpdateAttribute(ref xDoc, getterNode, "GlobalVariableName", varName);
                }
            }

            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                GlobalVariableGetterComponent getterComponent = base.Instantiate(xNode) as GlobalVariableGetterComponent;

                getterComponent.GlobalVariableNames = null;
                XmlNodeList getterNodes = xNode.SelectNodes("Getters/Getter");
                if (getterNodes != null && getterNodes.Count > 0)
                {
                    getterComponent.GlobalVariableNames = new List<string>();
                    foreach (XmlNode getterNode in getterNodes)
                    {
                        getterComponent.GlobalVariableNames.Add(getterNode.Attributes.GetNamedItem("GlobalVariableName").Value);
                    }
                }

                return getterComponent;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }
    }
}
